package com.example.phpclient.content;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.phpclient.R;

public class MyAdapter extends BaseAdapter {

    private Context context;
    private Cursor cursor;//数据库内容类型
    private LinearLayout layout;


    public MyAdapter(Context context, Cursor cursor){
        this.context = context;
        this.cursor =cursor;
    }

    @Override
    public int getCount() {
        return cursor.getCount();//cursor是个游标用getcount方法返回长度
    }

    @Override
    public Object getItem(int i) {
        return cursor.getPosition();
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater inflater =LayoutInflater.from(context);
        layout = (LinearLayout) inflater.inflate(R.layout.notelist_activity,null);
        //以以下的方式添加姓名、教室、是否签到控件,左侧
        TextView tv_name =(TextView)layout.findViewById(R.id.note_name);
        TextView tv_class =(TextView)layout.findViewById(R.id.note_class);
        TextView tv_qd =(TextView)layout.findViewById(R.id.note_qd);
        //以下控件添加图片，文字，视频
        TextView contenttv = (TextView) layout.findViewById(R.id.list_txt);//内容
        TextView timetv = (TextView) layout.findViewById(R.id.list_time);//时间
        ImageView imgiv = (ImageView) layout.findViewById(R.id.list_img);
        ImageView videoiv = (ImageView) layout.findViewById(R.id.list_video);//视频只显示第一张图片
        cursor.moveToPosition(i);//需要自己查询cursor

        String content = cursor.getString(cursor.getColumnIndex("content"));
        String time = cursor.getString(cursor.getColumnIndex("time"));
        String url =cursor.getString(cursor.getColumnIndex("path"));
        String urlvideo =cursor.getString(cursor.getColumnIndex("video"));

        String man_no =cursor.getString(cursor.getColumnIndex("man_no"));
        String man_class =cursor.getString(cursor.getColumnIndex("man_class"));

        contenttv.setText(content);
        timetv.setText(time);
        tv_class.setText(man_class);
        tv_name.setText(man_no);

        if (!(urlvideo.equals("null"))){
            videoiv.setImageBitmap(getVideoThumbnail(urlvideo,200,200, MediaStore.Images.Thumbnails.MICRO_KIND));
        }
        if (!(url.equals("null"))){
            imgiv.setImageBitmap(getImageThumbnail(url, 200 ,200));
        }

        return layout;
    }
    public Bitmap getImageThumbnail(String uri,int width,int height){

        Bitmap bitmap =null;
        //获取缩略图,为了呈现在listview中
        BitmapFactory.Options options =new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        bitmap = BitmapFactory.decodeFile(uri,options);
        options.inJustDecodeBounds = false;

        int beWidth = options.outWidth / width;
        int beHeight = options.outHeight / height;
        //以防跑出界外
        int be = 1;
        if (beWidth < beHeight){
            be =beWidth;
        }else
        {
            be = beHeight;
        }
        if (be<=0){
            be =1;
        }
        options.inSampleSize =be;
        bitmap =BitmapFactory.decodeFile(uri,options);
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width ,height ,
                ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        return bitmap;

    }
    private Bitmap getVideoThumbnail(String uri,int width, int height,int kind){
        Bitmap bitmap =null;
        bitmap = ThumbnailUtils.createVideoThumbnail(uri,kind);
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width ,height ,
                ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        return bitmap;
    }

}
